package q0189;

public class Solution3 {

    public static void rotate(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start += 1;
            end -= 1;
        }
    }

    public static void main(String[] args) {
        demo1(); // 5	0	1	2	3	4

    }

    private static void demo4() {
        int[] nums = new int[]{1, 2};

        rotate(nums, 3);

        print(nums);
    }

    private static void demo3() {
        int[] nums = new int[]{-1, -100, 3, 99};

        rotate(nums, 5);

        print(nums);

    }

    private static void demo2() {
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};

        rotate(nums, 3);

        print(nums);
    }

    private static void demo1() {

        int[] nums = new int[]{0, 1, 2, 3, 4, 5};

        rotate(nums, 1);

        print(nums);

    }

    public static void print(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            System.out.printf("%d\t", nums[i]);
        }
        System.out.println();
    }
}
